﻿using System;
using System.Windows;
using System.Collections;
using System.Windows.Controls;
using System.Windows.Input;
using Alex.WPFLibrary;
using Alex.DataLibrary.Enums;
using Alex.DataLibrary.Tables;

namespace Alex.ShipDataSaver
{
    /// <summary>
    /// Логика взаимодействия для MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void textBoxTableName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter) buttonAdd_Click(buttonAdd, new RoutedEventArgs());
        }

        private void buttonAdd_Click(object sender, RoutedEventArgs e)
        {
            if (textBoxTableName.Text.ToString() == "") return;

            tableName = textBoxTableName.Text.ToString();

            if (!listViewTables.Items.Contains(tableName))
                listViewTables.Items.Add(tableName);
            else
            {
                MessageBox.Show("This name is using in the tables list", "Causion", MessageBoxButton.OK);
                textBoxTableName.Clear();
                return;
            }

            TablesTypeDis tablesChange = new TablesTypeDis();

            tablesChange.Title = tableName;

            tablesChange.LabelTextBox = "Column name";

            tablesChange.LabelListView = "Columns";

            tablesChange.ShowDialog();

            TablesCollection.AddTable(tableName, (TableType)tablesChange.TypeOfTables, tablesChange.ColumnListView);

            textBoxTableName.Clear();
        }

        private void buttonDelete_Click(object sender, RoutedEventArgs e)
        {
            if (tableName.Length > 0)
                TablesCollection.DeleteTable(tableName);

            listViewTables.Items.Remove(listViewTables.SelectedItem);
        }

        private void listViewTables_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (listViewTables.SelectedItem != null)
                tableName = listViewTables.SelectedItem.ToString();
            else
                tableName = "";
            
            if (tableName.Length > 0)
                dataGrid.ItemsSource = TablesCollection.GetTable(tableName).DefaultView;
        }
        
        private void menuOpen_Click(object sender, RoutedEventArgs e)
        {
        	TablesCollection.ClearTables();
        	
            ArrayList items = TablesCollection.OpenTables();

            foreach (string name in items) listViewTables.Items.Add(name);
        }

        private void menuSave_Click(object sender, RoutedEventArgs e)
        {
            TablesCollection.SaveTables();
        }

        private void menuSaveAs_Click(object sender, RoutedEventArgs e)
        {
            TablesCollection.SaveTables();
        }

        private void buttonTreeAdd_Click(object sender, RoutedEventArgs e)
        {
            AddName addName = new AddName();

            addName.Title = "New name";

            addName.ShowDialog();

            TreeViewItem tvi = new TreeViewItem();

            tvi.Header = addName.ResultName;

            try
            {
                ((TreeViewItem)treeViewStruct.SelectedItem).Items.Add(tvi);
            }
            catch { }
        }

        private void buttonTreeDel_Click(object sender, RoutedEventArgs e)
        {
            object selectedObj = treeViewStruct.SelectedItem;
            DependencyObject parentObject;
            parentObject = ((TreeViewItem)selectedObj).Parent;
            try
            {
                ((TreeViewItem)parentObject).Items.Remove(selectedObj);
            }
            catch (InvalidCastException)
            {
                MessageBox.Show(this, "You cann't delete root level", "Error");
            }            
        }


        static string tableName;    //Храним здесь имя выбранной в списке таблицы.         
    }
}
